package com.bank.service;

import com.bank.api.model.AmountExchange;
import com.bank.api.model.PriceUnit;
import com.bank.api.model.Response;
import com.bank.exception.ExchangeException;
import com.bank.mapper.TbAmountMapper;
import com.bank.mapper.TbTrxRecordMapper;
import com.bank.model.TbAmount;
import com.bank.model.TbTrxRecord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@Slf4j
public class RMBBankTransferInnerService {


    @Autowired
    private TbAmountMapper tbAmountMapper;

    @Autowired
    private TbTrxRecordMapper tbTrxRecordMapper;

    // try
    //设计账户表，冻结资产表


    public Response transfer(AmountExchange amountExchange) {
        log.info("user {} try to  prepare exchange {} {}  to {} {}", amountExchange.getUserId(),
                amountExchange.getFrom(), amountExchange.getExchangeAmount(), amountExchange.getTo(), amountExchange.exchange());

        TbAmount tbAmount = tbAmountMapper.selectByUserId(amountExchange.getUserId());
        if (tbAmount == null) {
            log.error("无账户:{}", amountExchange.getUserId());

            throw new ExchangeException("无账户");
        }

        TbTrxRecord tbTrxRecord = new TbTrxRecord();


        //dollar 账户 减去
        if (amountExchange.getFrom() == PriceUnit.RMB) {
            if (tbAmount.getAmount() < amountExchange.getExchangeAmount()) {
                log.error("当前账户钱不够:{}", amountExchange.getTrxRecordNo());

                throw new ExchangeException("当前账户钱不够");
            }
            tbTrxRecord.setExchangeAmount(-amountExchange.getExchangeAmount());

            int ret = tbAmountMapper.decreaseAmount(amountExchange.getUserId(), amountExchange.getExchangeAmount(), tbAmount.getAmount());
            if (ret == 0) {
                log.error("当前账户扣钱失败:{}", amountExchange.getTrxRecordNo());

                throw new ExchangeException("当前账户扣钱失败");
            }
        } else {
            tbTrxRecord.setExchangeAmount(amountExchange.exchange());
            tbAmountMapper.increaseAmount(amountExchange.getUserId(), amountExchange.exchange());
        }

        tbTrxRecord.setTrxNo(amountExchange.getTrxRecordNo());
        tbTrxRecord.setUserId(amountExchange.getUserId());
        tbTrxRecord.setStatus(0);
        tbTrxRecord.setCreateTime(new Date());
        int insert = tbTrxRecordMapper.insert(tbTrxRecord);
        if (insert == 0) {
            log.error("流水记录失败:{}", amountExchange.getTrxRecordNo());

            throw new ExchangeException("流水记录失败");
        }

        return null;
    }

    /**
     * confirm 直接完成流水
     *
     * @param amountExchange
     * @return
     */
    public boolean confirm(AmountExchange amountExchange) {
        TbTrxRecord tbTrxRecord = new TbTrxRecord();
        tbTrxRecord.setTrxNo(amountExchange.getTrxRecordNo());
        tbTrxRecord.setUserId(amountExchange.getUserId());
        tbTrxRecordMapper.completed(tbTrxRecord);
        log.info("当前 流水:{},完成", amountExchange.getTrxRecordNo());
        return Boolean.TRUE;
    }

    public boolean cancel(AmountExchange amountExchange) {
        TbTrxRecord trxRecord = tbTrxRecordMapper.selectByTrxNo(amountExchange.getTrxRecordNo());
        if (trxRecord == null) {
            log.info("当前 当前流水不存在:{},不存在", amountExchange.getTrxRecordNo());
            return true;
        }

        TbTrxRecord tbTrxRecord = new TbTrxRecord();
        tbTrxRecord.setTrxNo(amountExchange.getTrxRecordNo());
        tbTrxRecord.setUserId(amountExchange.getUserId());
        tbTrxRecordMapper.canceled(tbTrxRecord);
        tbAmountMapper.increaseAmount(amountExchange.getUserId(), trxRecord.getExchangeAmount());
        log.info("当前 流水:{},取消", amountExchange.getTrxRecordNo());
        return Boolean.TRUE;
    }
}
